﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Permissions.Services.Domain;
using Permissions.Services.Domain.Queries;

namespace Permissions.Services.Domain.QueryHandlers
{
    public class UserAllowedQueryHandler
    {
        public Task<bool> Handle(IsUserAllowedQuery request, CancellationToken cancellationToken)
        {
            if (request.UserId == Guid.Empty || request.UserId == default)
            {
                return Task.FromResult(false);
            }

            if (request.UserId == DomainModule.AdminId)
            {
                return Task.FromResult(true);
            }

            // TODO: query database here
            if (request.Permission?.StartsWith("READ", StringComparison.OrdinalIgnoreCase) == true)
            {
                return Task.FromResult(true);
            }

            return Task.FromResult(false);
        }
    }
}
